package cn.b.sky.mes.em.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.em.dao.EquipmentCategoryDao;
import cn.b.sky.mes.em.model.EquipmentCategory;
import cn.b.sky.mes.em.model.condition.EquipmentCategoryCondition;
import cn.b.sky.mes.em.service.EquipmentCategoryService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * 
 * <strong>Title : EquipmentCategoryServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.em.service.impl<br></strong>
 * <strong>Description : </strong>设备类别业务实现类<br> 
 * <strong>Create on : 2015-11-2 下午02:12:11<br></strong>
 * <p>
 * @author zhaoxin01<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人 | 修改日期 | 修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("equipmentCategoryService")
public class EquipmentCategoryServiceImpl implements EquipmentCategoryService {

  private static final Logger LOG = LoggerFactory.getLogger(EquipmentCategoryServiceImpl.class);

  @Resource
  EquipmentCategoryDao equipmentCategoryDao;

  @Override
  public List<EquipmentCategory> selcetEquipmentCategoryByCondition(
      EquipmentCategoryCondition equipmentCategoryCondition) {
    return equipmentCategoryDao.selcetEquipmentCategoryByCondition(equipmentCategoryCondition);
  }

  @Override
  public int selcetEquipmentCategoryCount(EquipmentCategoryCondition equipmentCategoryCondition) {
    return equipmentCategoryDao.selcetEquipmentCategoryCount(equipmentCategoryCondition);
  }

  @Override
  public EquipmentCategory selectEquipmentCategoryByCode(String siteCode, String categoryCode) {
    return equipmentCategoryDao.selectEquipmentCategoryByCode(siteCode, categoryCode);
  }

  @Override
  public void insertEquipmentCategory(EquipmentCategory equipmentCategory) {
    equipmentCategoryDao.insertEquipmentCategory(equipmentCategory);

  }

  @Override
  public void updateEquipmentCategory(EquipmentCategory equipmentCategory) {
    equipmentCategoryDao.updateEquipmentCategory(equipmentCategory);

  }

  @Override
  public BatchJobReport deleteEquipmentCategory(String siteCode, String categoryCode) {
    String cause = "";
    Boolean success = true;
    try {
      equipmentCategoryDao.deleteEquipmentCategory(siteCode, categoryCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        //外键删除已存在提示
        cause = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
            SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),
                SecurityContextUtil.getLanguageCode(), "notDelete.M");
      } else {
        cause = e.getMessage();
      }
      success = false;
    }
    return new BatchJobReport(categoryCode, cause, success);
  }

  @Override
  public List<EquipmentCategory> selcetEquipmentCategoryForSuggest(EquipmentCategoryCondition equipmentCategoryCondition) {
    return equipmentCategoryDao.selcetEquipmentCategoryForSuggest(equipmentCategoryCondition);
  }

}
